﻿//时间 几天前 几分钟前
function getDateTimeStamp(dateStr){
    dateStr=dateStr.replace(/T/g,' ');
    dateStr=dateStr.replace(/\.[0-9]+/g,'');
    return Date.parse(dateStr.replace(/-/gi,"/"));
}
function getDateDiff(dateTimeStamp){
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var halfamonth = day * 15;
    var month = day * 30;
    var now = new Date().getTime();
    var diffValue = now - dateTimeStamp;
    if(diffValue < 0){return;}
    var monthC =diffValue/month;
    var weekC =diffValue/(7*day);
    var dayC =diffValue/day;
    var hourC =diffValue/hour;
    var minC =diffValue/minute;
    if(monthC>=1){
        result="" + parseInt(monthC) + "月前";
    }
    else if(weekC>=1){
        result="" + parseInt(weekC) + "周前";
    }
    else if(dayC>=1){
        result=""+ parseInt(dayC) +"天前";
    }
    else if(hourC>=1){
        result=""+ parseInt(hourC) +"小时前";
    }
    else if(minC>=1){
        result=""+ parseInt(minC) +"分钟前";
    }else
        result="刚刚";
    return result;
}
//-----------------------------------------------------------------
var regs=/(searchKey=)([^\s]+)/;
var searchKey="";
if(urlfrom.match(regs)&&urlfrom.match(regs).splice("=")[2]){
    searchKey=urlfrom.match(regs).splice("=")[2];
}else if(!urlfrom.match(regs)){
    $("#waiting").css("display","none");
    $("#nonews").css("display","block");
}
if(searchKey.match("%")){
    searchKey=decodeURIComponent(searchKey);
}
$("#page_title1").html(searchKey);
if(searchKey&&searchKey.length>0){
    $("span.query").html(searchKey);
}else{
    $("h2 .query_wrapper").css("display","none");
}

var currentIndex1=1;
//页面切换
$("#choicesearchgoback").click(function(){
    $("#choicesearch").css("display","none");
    $("#page_wrapper1").css("display","block");
});
$("#citygobackbtn").click(function(){
    $("#ctchoicepage").css("display","none");
    $("#page_wrapper").css("display","block");
});
//经验金钱判断能否进入------------------------------------
function moneytest(e){
    //判断能不能去看详情页
    var euserid= e.dataset.euser;
    var thiseid= e.dataset.eid;
    var score=e.dataset.score;
    $.ajax({
        type:"post",
        datatype:"json",
        url:textexpurl,
        data:{"quserId":euserid,"contentId":thiseid,"userId":id,"score":score},
        success:function(d){
            console.log(d);
            if(d.Message=="自己的经验"||d.Message=="已付费"||d.Message=="未付费"){
                $.cookie("paynum",thiseid);
                location.href='shareexppage.html?eid='+thiseid;
            }else{
                $("body").css("overflow","hidden");
                $(".moneyinfomob").css("display","block");
            }
        }
    });
}
$().ready(function(){
    if($.cookie("GJstype")){
        stype=$.cookie("GJstype");
        hobby=$.cookie("GJhoddy");
        job=$.cookie("GJjob");
        city=$.cookie("GJcity");
        education=$.cookie("GJeducation");
        relation=$.cookie("GJrelation");
        currentIndex2=$.cookie("GJcurrent");
        choiced=1;
        chiocesearchajax(stype,hobby,job,city,education,relation,currentIndex2);
    }
    else{
        aj(currentIndex1)
    }
});
var fromeurl=document.referrer;
console.log(fromeurl);
function aj(currentIndex1){
    //searchKey=encodeURIComponent(searchKey);
    if(listajax===0&&searchKey){
        $.ajax({
            type:"get",
            datatype:"json",
            url:choicesearchurl+"?searchKey="+searchKey+"&userId="+id+"&currentIndex="+currentIndex1+"&pageSize=6",
            success:function(d){
                console.log(d);
                if(d.Data&&d.Data.length>0){
                    if(d.Data.length<6){
                        listajax=1;
                        $("#waiting").css("display","none");
                        $("#nonews").css("display","block");
                        $("#nonews").html("没有更多搜索结果");
                    }
                    for(var i=0;i<d.Data.length;i++){
                        var regt=/([0-9\-]+)/;
                        var tim=getDateDiff(getDateTimeStamp(d.Data[i].ctime));
                        var hrefto="";
                        if(d.Data[i].user){
                            if(d.Data[i].user.userId===id){
                                hrefto="setpage.html";
                            }else{
                                hrefto="otherpeoplepage.html?otherid="+d.Data[i].user.userId;
                            }
                        }
                        if(!tim){
                            tim="刚刚";
                        }
                        //新闻
                        if(d.Data[i].contenttype===1){
                            $(".searchlist-box").append("<div class='feed_item'> <div class='title'> <a class='BoardItemTitle'>"+d.Data[i].name+"</a> </div><div class='EventHeader'> <span><a>"+d.Data[i].fromsite+"</a></span> <span class='timestamp'> · "+tim+"</span> </div><div class='answer_count_row'> <a href='"+d.Data[i].link+"'>查看详情</a> </div> </div>");
                        }
                        //问题 <span class='TopicNameSpan TopicName'> · 密友</span>   <div class='preview_only'> <div>"+d.Data[i].content+"</div> </div>
                        if(d.Data[i].contenttype===2){
                            $(".searchlist-box").append("<div class='feed_item'> <div class='title'> <a class='BoardItemTitle'>"+d.Data[i].name+"</a> </div> <div class='EventHeader'> <span><a "+hrefto+">"+d.Data[i].user.nick+"</a></span>  <span class='timestamp'> · "+tim+"</span> <span class='scoreimg'><img src='../img/wen01.png'><img src='../img/money04.png'/>"+parseFloat(d.Data[i].score)/100+"</span><div class='answer_count_row'> <a href='questiondetail.html?qid="+ d.Data[i].contentId+"'>查看详情</a> </div> </div> </div>");
                        }
                        //经验 <span class='TopicNameSpan TopicName'> · 密友</span>  <div class='preview_only'> <div>"+d.Data[i].content+"</div>
                        if(d.Data[i].contenttype===3){
                            console.log("经验ID："+d.Data[i].contentId);
                            $(".searchlist-box").append("<div class='feed_item'> <div class='title'> <a class='BoardItemTitle'>"+d.Data[i].name+"</a> </div> <div class='EventHeader'> <span><a "+hrefto+">"+d.Data[i].user.nick+"</a></span> <span class='timestamp'> · "+tim+"</span> <span class='scoreimg'><img src='../img/jing01.png'><img src='../img/money04.png'/>"+parseFloat(d.Data[i].score)/100+"</span></div> <div class='answer_count_row'> <a  onclick='moneytest(this)' data-euser='"+d.Data[i].user.userId+"' data-score='"+d.Data[i].score+"' data-eid='"+d.Data[i].contentId+"'>查看详情</a> </div></div>  </div>");
                        }
                    }
                }else{
                    listajax=1;
                    $("#waiting").css("display","none");
                    $("#nonews").css("display","block");
                }

            }
        })
    }
}
//var choiced=0;//点击过筛选为1
//筛选按钮 页面切换 高级搜索
$("#gochoicebtn").click(function(){
    //清空上次残留数据
    choiceajax=0;//筛选列表有数据0 清空筛选状态
    hyischoiced=[];//已经选择的行业
    xqischoiced=[];//已经选择的兴趣
    stype="";//筛选类型
    relation="";//关系
    hobby="";//兴趣ID
    job="";//行业ID
    city="";//定位
    education="";//教育

    $(".eduwords").html("");//教育
    $("#relawords").html("");//关系
    $(".myctywords span").remove();//定位
    $(".hangyewords span").remove();//行业
    $(".xingquwords span").remove();//兴趣
    $("#choicedbox span").remove();//行业已经选择
    $("#xqchoicedbox span").remove();//兴趣已经选择

    var edokbtnimg2=document.getElementsByClassName("edokbtnimg2");//学历选中按钮图片2 未选
    var edokbtnimg1=document.getElementsByClassName("edokbtnimg1");//学历选中按钮图片1
    for(var i1=0;i1<edokbtnimg2.length;i1++){
        edokbtnimg2[i1].style.display="block";
        edokbtnimg1[i1].style.display="none";
    }

    $("#page_wrapper1").css("display","none");
    $("#choicesearch").css("display","block");
    $("#waiting").css("display","block");//消息提醒
    $("#nonews").css("display","none");
});
$(window).scroll(function(){
    var mainheight=$(".searchlist-box").height();
    var windowheight=$(window).height();
    var chajuli=$(".searchlist-box").offset().top - $(window).scrollTop();
    var scrollheight=$(window).scrollTop();
    var cz=windowheight-mainheight+5;
    if(chajuli<cz){
        if($("#page_wrapper1").css("display")==="block"&&choiced===0){
            currentIndex1++;
            aj(currentIndex1);
        }else if($("#page_wrapper1").css("display")==="block"&&choiced===1){
            currentIndex2++;
            chiocesearchajax(stype,hobby,job,city,education,relation,currentIndex2);
        }
    }
});